package mipo.top.kaoshi.controller;

import mipo.top.base.util.JsonResult;
import mipo.top.base.util.PageUtil;
import mipo.top.kaoshi.domain.DictionaryType;
import mipo.top.kaoshi.query.DictionaryTypeQuery;
import mipo.top.kaoshi.service.IDictionaryDetailService;
import mipo.top.kaoshi.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictype")
public class DictionaryTypeController {

    @Autowired
    private IDictionaryTypeService dictionaryTypeService;
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    @GetMapping
    public List<DictionaryType> sad(){
        return dictionaryTypeService.findall();
    }

    @PutMapping
    public JsonResult su(@RequestBody DictionaryType dictionaryType){
        try {
            if(dictionaryType.getId()==null){
                dictionaryTypeService.save(dictionaryType);
            }else{
                dictionaryTypeService.update(dictionaryType);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("错误数据");
        }
    }

    @DeleteMapping("/{id}")
    public JsonResult adw(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.deleteTypes(id);
            dictionaryTypeService.remove(id);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("出错了");
        }
        return JsonResult.me();
    }

    @PostMapping
    public PageUtil<DictionaryType> asp(@RequestBody DictionaryTypeQuery dictionaryTypeQuery){
        return dictionaryTypeService.queryData(dictionaryTypeQuery);
    }
    @PatchMapping
    private void bacthDel(@RequestBody List<Long> ids){
        dictionaryDetailService.patchremoveType(ids);
        dictionaryTypeService.patchremove(ids);
    }
}
